from flask import Blueprint, request, jsonify
from ..controllers.house import create_house, get_houses, get_house, update_house, delete_house

house_bp = Blueprint('house', __name__, url_prefix='/house')


@house_bp.route('/add', methods=['POST'])
def create_house_route():
    """创建房屋"""
    try:
        data = request.get_json() or {}
        return create_house(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})


@house_bp.route('/list', methods=['GET'])
def get_houses_route():
    """获取房屋列表"""
    try:
        data = request.args.to_dict() or {}
        return get_houses(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})


@house_bp.route('/house', methods=['GET'])
def get_house_route():
    """获取单个房屋信息"""
    try:
        data = request.args.to_dict() or {}
        return get_house(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})


@house_bp.route('/edit', methods=['POST'])
def update_house_route():
    """更新房屋信息"""
    try:
        data = request.get_json() or {}
        return update_house(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})


@house_bp.route('/delete', methods=['POST'])
def delete_house_route():
    """删除房屋"""
    try:
        data = request.get_json() or {}
        return delete_house(data)
    except Exception as e:
        return jsonify({'code': 500, 'message': '服务器内部错误', 'data': str(e)})